<template>
  <div class="app-container">
    <div style="width:100%;height:30px;display: flex; justify-content: flex-end;">
      <el-button
          size="mini"
          type="primary" round
          @click="goToBack()"
      >返回上一页
      </el-button>
    </div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="共保人名称" prop="baoxianId" >
        <!--<el-input v-model="form.baoxianId" placeholder="请输入共保人id" />-->
        <el-select v-model="queryParams.baoxianId" placeholder="请选择共保人" clearable>
          <el-option
              v-for="dict in baoxianList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目负责人" prop="responsiblePerson">
        <el-input
          v-model="queryParams.responsiblePerson"
          placeholder="请输入项目负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['insurance:coinsurance:add']"
        >新增</el-button>
      </el-col>
        <!--<el-col :span="1.5">-->
        <!--  <el-button-->
        <!--    type="success"-->
        <!--    plain-->
        <!--    icon="el-icon-edit"-->
        <!--    size="mini"-->
        <!--    :disabled="single"-->
        <!--    @click="handleUpdate"-->
        <!--    v-hasPermi="['insurance:coinsurance:edit']"-->
        <!--  >修改</el-button>-->
        <!--</el-col>-->
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['insurance:coinsurance:remove']"
          >删除</el-button>
        </el-col>
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="warning"-->
      <!--    plain-->
      <!--    icon="el-icon-download"-->
      <!--    size="mini"-->
      <!--    @click="handleExport"-->
      <!--    v-hasPermi="['insurance:coinsurance:export']"-->
      <!--  >导出</el-button>-->
      <!--</el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="coinsuranceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="共保人名称" align="center" prop="baoxianName" />
      <el-table-column label="承保比例" align="center" prop="rate" />
      <el-table-column label="项目负责人" align="center" prop="responsiblePerson" />
      <el-table-column label="联系方式" align="center" prop="tel" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['insurance:coinsurance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['insurance:coinsurance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改保险合同共保信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="共保人" prop="baoxianId">
          <!--<el-input v-model="form.baoxianId" placeholder="请输入共保人id" />-->
          <el-select v-model="form.baoxianId" placeholder="请选择共保人" clearable>
            <el-option
                v-for="dict in baoxianList"
                :key="dict.deptId"
                :label="dict.deptName"
                :value="dict.deptId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="承保比例" prop="rate">
          <el-input v-model="form.rate" placeholder="请输入承保比例" />
        </el-form-item>
        <el-form-item label="项目负责人" prop="responsiblePerson">
          <el-input v-model="form.responsiblePerson" placeholder="请输入项目负责人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系方式" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCoinsurance, getCoinsurance, delCoinsurance, addCoinsurance, updateCoinsurance } from "@/api/insurance/coinsurance";
import {isEmpty, isValidDecimal} from "@/utils/validate";
import {getBaoxianlist} from "@/api/common/common";

export default {
  name: "Coinsurance",
  data() {
    const checkRateRel = (rule, value, callback) => {
      if (isEmpty(value)) {
        callback();
        return
      }

      if (value > 100) {
        callback(new Error('回款比例不能大于100'));
        return;
      }
      if (value < 0) {
        callback(new Error('回款比例不能小于0'));
        return;
      }
      if (!isValidDecimal(value)) {
        callback(new Error('请输入正确的比例，小数不能超过2位'));
        return;
      }
      callback();
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保险合同共保信息表格数据
      coinsuranceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        insureId: null,
        contractId: null,
        baoxianId: null,
        rate: null,
        responsiblePerson: null,
        tel: null,
      },
      baoxianList:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        baoxianId: [
          {required: true, message: "共保人不能为空", trigger: "blur"}
        ],
        rate:[
          {validator: checkRateRel, trigger: 'blur'}
        ]
      },
      contractId: null,
      insureId: null
    };
  },
  created() {
    this.getBaoXian()
    this.contractId = this.$route.query.contractId
    this.insureId = this.$route.query.insureId
    if (isEmpty(this.contractId) || isEmpty(this.insureId)) {
      this.$modal.msgWarning('缺少必要参数，请重新进入页面')
      return
    }
    this.getList();
  },
  methods: {
    goToBack() {
      this.$router.push({path: "/bxht/contract"});
    },
    getBaoXian(){
      getBaoxianlist().then().then(res => {
        res.data.forEach(item => {
          item.deptId = String(item.deptId)
        });
        this.baoxianList = res.data
      })
    },
    /** 查询保险合同共保信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.contractId = this.contractId
      this.queryParams.insureId = this.insureId
      listCoinsurance(this.queryParams).then(response => {
        this.coinsuranceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        insureId: null,
        contractId: null,
        baoxianId: null,
        rate: null,
        responsiblePerson: null,
        tel: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保险合同共保信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCoinsurance(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保险合同共保信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.contractId = this.contractId
          this.form.insureId = this.insureId
          if (this.form.id != null) {
            updateCoinsurance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCoinsurance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的保险合同共保信息？').then(function() {
        return delCoinsurance(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('insurance/coinsurance/export', {
        ...this.queryParams
      }, `coinsurance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
